package uk.co.kevinlockwood.kol.droid.activities;

import uk.co.kevinlockwood.kol.droid.DroidHelpers;
import uk.co.kevinlockwood.kol.droid.G;
import uk.co.kevinlockwood.kol.droid.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class Options extends Activity implements OnClickListener {
	private SharedPreferences settings;
	private Integer fontsize;
	private Button plusButton;
	private Button minusButton;
	private TextView fontsize_text;
	private CheckBox fullscreen;
	private CheckBox keepscreenon;
	private Button forgetPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		DroidHelpers.trace("Chat.onCreate()");
		super.onCreate(savedInstanceState);
		try {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			settings = getSharedPreferences(G.PREFS_NAME, MODE_PRIVATE);
			setContentView(R.layout.options);
		} catch (Exception e) {
			DroidHelpers.printStackTrace(e);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		DroidHelpers.trace("Options.onStart()");
		DroidHelpers.setupActivity(this, settings);

		fullscreen = (CheckBox)findViewById(R.id.checkbox_fullscreen);
		fullscreen.setChecked(settings.getBoolean(G.FULL_SCREEN, Boolean.TRUE));
		fullscreen.setOnClickListener(this);

		keepscreenon = (CheckBox)findViewById(R.id.checkbox_keepscreenon);
		keepscreenon.setChecked(settings.getBoolean(G.KEEP_SCREEN_ON, Boolean.FALSE));
		keepscreenon.setOnClickListener(this);

		fontsize = settings.getInt(G.FONT_SIZE, 18);
		plusButton = (Button)findViewById(R.id.button_fontsize_plus);
		minusButton = (Button)findViewById(R.id.button_fontsize_minus);
		fontsize_text = (TextView)findViewById(R.id.button_fontsize_label);
		setFontSize();
		plusButton.setOnClickListener(this);
		minusButton.setOnClickListener(this);

		forgetPassword = (Button)findViewById(R.id.button_forget_password);
		if (settings.getBoolean(G.AUTO_LOG_IN, Boolean.FALSE)) {
			forgetPassword.setOnClickListener(this);
		} else {
			forgetPassword.setEnabled(Boolean.FALSE);
		}
	}

	private void setFullScreen() {
		Boolean fullscr = fullscreen.isChecked();
		settings.edit().putBoolean(G.FULL_SCREEN, fullscr).commit();
		if (fullscr) {
			getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
		} else {
			getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN);
		}
	}

	private void setKeepScreenOn() {
		Boolean keepscron = keepscreenon.isChecked();
		settings.edit().putBoolean(G.KEEP_SCREEN_ON, keepscron).commit();
		if (keepscron) {
			getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		} else {
			getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
		}
	}

	private void setFontSize() {
		settings.edit().putInt(G.FONT_SIZE, fontsize).commit();
		fontsize_text.setTextSize(fontsize);
	}

	private void forgetPassword() {
		settings.edit().putBoolean(G.AUTO_LOG_IN, Boolean.FALSE)
			.remove(G.PASS_WORD).commit();
		forgetPassword.setEnabled(Boolean.FALSE);
	}

	@Override
	protected void onDestroy() {
		DroidHelpers.trace("Options.onDestroy()");
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		DroidHelpers.trace("Options.onStop()");
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		DroidHelpers.log(v, v.getId());
		switch (v.getId()) {
		case R.id.button_fontsize_plus:
			fontsize += 1;
			setFontSize();
			break;
		case R.id.button_fontsize_minus:
			fontsize -= 1;
			setFontSize();
			break;
		case R.id.checkbox_fullscreen:
			setFullScreen();
			break;
		case R.id.checkbox_keepscreenon:
			setKeepScreenOn();
			break;
		case R.id.button_forget_password:
			forgetPassword();
			break;
		}
	}
}
